<?php


namespace app\system\controller\cms;

use app\common\model\cms\ClassModel;
use app\common\model\cms\DetailModel;
use app\system\controller\BaseController;

class DetailController extends BaseController
{

  /**
   * 不检测权限方法
   * @var array
   */
  protected $noCheck = [];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [
      '*' => 'index',
  ];

  /**
   * 当前模型
   * @var null
   */
  protected $model = null;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new DetailModel();
  }

  /**
   * 列表
   * @return array|string
   * @throws \Exception
   */
  public function index()
  {
    if ($this->request->isAjax()) {
      $map        = [];
      $classModel = new ClassModel();
      $classList  = $classModel->getList();
      $classId    = $this->request->param('class_id/d');
      $tableName  = 'article';
      if ($classId > 0) {
        $classInfo = $classList[$classId];
        $tableName = $classInfo['table_name'];
        $map[]     = ['class_id', 'in', (string)$classInfo['path']];
      }
      $this->request->filter(['strip_tags']);
      $pageSize   = $this->request->param('pageSize/d', 20); //每页多少条
      $pageNumber = $this->request->param('pageNumber/d'); //当前页
      $sortName   = $this->request->param('sortName'); //排序字段
      $sortOrder  = $this->request->param('sortOrder', 'desc'); //升序降序
      $order      = !empty($sortName) ? "{$sortName} {$sortOrder}" : ''; //排序
      $title      = $this->request->param('title');
      if (!empty($title)) $map[] = ['title', 'like', '%' . $title . '%'];
      try {
        $data          = ['total' => 0, 'rows' => []];
        $data['total'] = $this->model->setSuffix($tableName)->where($map)->count();
        if ($data['total'] > 0) {
          $list         = $this->model->setSuffix($tableName)
              ->where($map)
              ->order($order)
              ->page((int)$pageNumber, (int)$pageSize)
              ->select();
          $data['rows'] = $list;
        }
        $data['sql'] = $this->model->setSuffix($tableName)->getLastsql();
      } catch (\Exception $e) {
        $data = ['total' => 0, 'rows' => [], 'err_msg' => $e->getMessage()];
      }
      return $data;
    }
    return $this->fetch();
  }

  /**
   * 添加
   * @return string
   * @throws \Exception
   */
  public function add()
  {
    if ($this->request->isAjax()) {
      $data      = $this->request->param();
      $validRule = $this->model->getValidRule($data['table_name'], 'add');
      if (!empty($validRule)) $this->validate($data, $validRule);
      $data['uid'] = $this->loginInfo['uid'];
      $this->model->add($data);
      $this->success('操作成功');
    }
    $tableName = $this->request->param('table_name', 'article');
    $form      = $this->model->getForm($tableName);
    $this->assign('table_name', $tableName);
    $this->assign('form', $form);
    return $this->fetch();
  }

  /**
   * 编辑
   * @return string
   * @throws \Exception
   */
  public function edit()
  {
    if ($this->request->isAjax()) {
      $data      = $this->request->param();
      $validRule = $this->model->getValidRule($data['table_name'], 'edit');
      if (!empty($validRule)) $this->validate($data, $validRule);
      $this->model->edit($data);
      $this->success('操作成功');
    }
    $tableName = $this->request->param('table_name');
    $id        = $this->request->param('id/d');
    $info      = $this->model->setSuffix($tableName)->where('id', $id)->find();
    $form      = $this->model->getForm($tableName, $info);
    $this->assign('table_name', $tableName);
    $this->assign('form', $form);
    $this->assign('info', $info);
    return $this->fetch();
  }

  /**
   * 删除内容
   * @throws \Exception
   */
  public function del()
  {
    $tableName = $this->request->param('table_name');
    $id        = $this->request->param('id/s');
    $this->model->del($tableName, $id);
    $this->success('操作成功');
  }

}